package com.hzit.servlet.emp;

import com.hzit.bean.Emp;
import com.hzit.dao.EmpDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "FindEmpByIdServlet", urlPatterns = "/emp/find")
public class FindEmpByIdServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取empno
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String id = request.getParameter("empno");
        Integer empno = (id != null) ? Integer.parseInt(id) : -1;

        //2.根据empno查询 emp对象
        EmpDao empDao = new EmpDao();

        Emp emp = empDao.findEmpByPrimary(empno);

        if (emp == null) {
            System.err.println("员工编号查找失败!");
            response.sendRedirect(request.getContextPath() + "/emp/all");
            return;
        }
        //3.把数据存放域中  跳转到修改页面，进行数据的回显
        request.setAttribute("emp", emp);

        request.getRequestDispatcher("/jsp/EmpUpdate.jsp").forward(request, response);


    }
}
